package com.action.app_center;

import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.json.JSONArray;

import com.enetiy.Areainfo;
import com.enetiy.Areaip;
import com.enetiy.Areausable;
import com.enetiy.Binding;
import com.enetiy.Useraccount;
import com.enetiy.Userinfo;
import com.enetiy.Users;
import com.opensymphony.xwork2.ActionSupport;
import com.operation.HotspotLink;
import com.operation.NET;
import com.service.AreaIpService;
import com.service.AreaService;
import com.service.AreainfoService;
import com.service.AreausbleService;
import com.service.BindingService;
import com.service.LoginService;
import com.service.UseraccountService;
import com.service.UserinfoService;
import com.util.DataUtil;
import com.util.IPUtil;
import com.util.Log4gUtil;

public class BindingAction extends ActionSupport { 
	private BindingService bindingService;
	private  UserinfoService userinfoService ;
	private  AreausbleService areausbleService;
	private AreaService areaService;
	private AreainfoService areainfoService;
	private LoginService loginService;
	private AreaIpService areaIpService;
	private UseraccountService useraccountService;
	
	private String account;//绑定的学号
	private String accountPwd;//密码
	private String ip;
	private String params;
	private String token;
	private int userid;
	private int areaid;
	private int siteAreaid;
	private String NF_callid;
	private int areaiden; //默认1
	private int bindingid;
	private HotspotLink hotspotLink = new HotspotLink();
	
	
	/**
	 * 
	 * 添加绑定 >Nf023
	 */
	public void addBinding(){
		long startTime = new Date().getTime();
		if(params!=null&&!params.equals("")){
			org.json.JSONObject datajson = DataUtil.dataParser(params);
			if(datajson.has("account"))
				account=datajson.getString("account");
			if(datajson.has("accountPwd"))
				accountPwd = datajson.getString("accountPwd");
			if(datajson.has("userid"))
				userid = datajson.getInt("userid");
			if(datajson.has("bindingareaid"))
				areaid = datajson.getInt("bindingareaid");
			if(datajson.has("areaiden"))
				areaiden = datajson.getInt("areaiden");
			}
		Log4gUtil.Info("addBinding-添加绑定", userid+"","进入params", params, NF_callid, token);
		
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = null;
		net.sf.json.JSONObject jo = DataUtil.sendJson();	
		try {
			out = response.getWriter();
				//区域信息
				Areainfo areainfo = areaService.selsctAreainfo(areaid,areaiden);
				Users user = loginService.selectUserId(userid);
				Binding di=null;//已绑定的binding
				//验证区域可用
				if(areainfo != null && user != null){
					Areaip areaip = areaIpService.selectAreaip(areaid);
					//验证绑定
					if(hotspotLink.hotspotVerify(account, accountPwd,areaid,areainfo.getVerifyUrl(),areaip.getPublicIp(),userid,NF_callid,token)==1){
						
						//如此牛郎账号已有绑定此区域;
						if((di=bindingService.selectbinding(account, areaid))!=null){
							Log4gUtil.getLog().info("addBinding-userid:"+userid+"-token:"+token+"-此绑定账号已有绑定,解除原绑定操作");
							//已绑定的binding 的用户信息
							Userinfo uin = userinfoService.selectUserinfo(di.getUsers().getId());
							// 如此binding 正好有用户在使用
							//修改状态
							uin.setNetOnline(0);
							uin.setBinding(null);
							userinfoService.updateUserinfo(uin);
							bindingService.deleteBingding(di);//删除
						}
						//此绑定账号已有牛郎账号绑定
						if((di=bindingService.selectbinding(userid, areaid))!=null){
							//已绑定的binding 的用户信息
							Userinfo uin = userinfoService.selectUserinfo(di.getUsers().getId());
							// 如此binding 正好有用户在使用
							//修改状态
							uin.setNetOnline(0);
							uin.setBinding(null);
							userinfoService.updateUserinfo(uin);
							bindingService.deleteBingding(di);//删除
						}
						
							Binding bin = new Binding();
							
							if(areaid==2){
								if(user.getNickname()!=null&&user.getNickname().equals("未录入")
										&&hotspotLink.name!=null&&!hotspotLink.name.equals(""))
									user.setNickname(hotspotLink.name);
								
								Areainfo teacher = areainfoService.selectAreainfo(areaid, "NF-net-00");
								Log4gUtil.Info("addBinding-添加绑定", userid+"","绑定type", hotspotLink.type+"--"+teacher, NF_callid, token);
								if(hotspotLink.type!=null&&teacher!=null){
									Log4gUtil.Info("addBinding-添加绑定", userid+"","进入老师绑定", hotspotLink.type, NF_callid, token);
									if(hotspotLink.type.equals("老师")){
										bin.setAreainfo(teacher);
									}else{
										bin.setAreainfo(areainfo);
									}
									bin.setTypeExplain(hotspotLink.type);
								}else{
									bin.setAreainfo(areainfo);
								}
							}else{
								bin.setAreainfo(areainfo);
							}
							
							bin.setAccount(account);
							bin.setAccountPwd(accountPwd);
							bin.setArea(areainfo.getArea());
							bin.setBindingTime(new Date());
							bin.setUsers(user);
							//添加
							bindingService.insertBinding(bin);//重新绑定	
							loginService.updateUser(user);
							
							jo.put("code", 1);
				}else{//绑定账号或密码错误
					jo.put("code", 0);
					jo.put("reason",1);
					jo.put("msg",hotspotLink.msg);
				}
			}else{
				jo.put("code", -1);
				jo.put("msg","绑定区域未知");
			}
		} catch (Exception e){
			jo.put("code", -1);
			jo.put("msg","服务器异常");
			e.printStackTrace();
		}
		Long endTime = new Date().getTime();
		Log4gUtil.Info("addBinding-添加绑定", userid+"","输出时间", (startTime-endTime)+"-"+jo.toString(), NF_callid, token);
		out.print(jo);
	}
	
	
	
	
	/*************************************************************
	 *  解除绑定 >Nf024
	 */
	public void relieveBinding(){
		if(params!=null&&!params.equals("")){
			org.json.JSONObject datajson = DataUtil.dataParser(params);
			if(datajson.has("bindingid"))
				bindingid = datajson.getInt("bindingid");
		}
		Log4gUtil.Info("relieveBinding-解除绑定", userid+"", "进入params", params, NF_callid, token);
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = null;
		net.sf.json.JSONObject json = DataUtil.sendJson();
		try {
			out = response.getWriter();
			Binding di=null;
			//验证是否有绑定
			if((di=bindingService.selectBinding(bindingid))!=null){
				Areaip areaip = areaIpService.selectAreaip(di.getArea().getId());
					//	已绑定的binding 的用户信息
					Userinfo uin = userinfoService.selectUserinfo(di.getUsers().getId());
					// 如此binding 正好有用户在使用
					if(uin.getBinding()!=null&&uin.getBinding().getId()==di.getId()){
						areaip = areaIpService.selectAreaip(uin.getBinding().getArea().getId());
						//用户下线
						List<Useraccount> accounts = useraccountService.selectUseraccounts(uin.getUsers().getId(), 1);
						if(!accounts.isEmpty()){
							NET net = new NET();	
							net.netIntercept(uin.getArea().getId(),uin.getUserIp(),uin.getArea().getNetOnline(),
									areaip.getPublicIp(),uin.getUserMac(),uin.getUsers().getId(),accounts.get(0).getAccount(),NF_callid,token,"");
						}
						//修改状态
						uin.setNetOnline(0);
					}
					uin.setBinding(null);
					userinfoService.updateUserinfo(uin);
					bindingService.deleteBingding(di);//删除
					json.put("code", 1);
			}else{
				json.put("code", 0);
			}
			out.print(json);
		} catch (Exception e) {
			json.put("code", -1);
			out.print(json);
		}
		Log4gUtil.Info("relieveBinding-解除绑定", userid+"", "输出", json.toString(), NF_callid, token);
	}
	
	
	/**
	 * 用户所在区域所有可绑绑定  >Nf026
	 */
	public void areaList(){
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = null;
		JSONObject sendjson = DataUtil.sendJson();
		try {
			out = response.getWriter();
			JSONArray jsonList = new JSONArray();
			//获取此区域可用绑定
			List<Areausable> selectAreabList = areausbleService.selectAreabList(siteAreaid,1);
			String pic = IPUtil.getIp()+"/icon/bangding/";
			if(!selectAreabList.isEmpty()){
				for (int i = 0; i < selectAreabList.size(); i++){
					Areainfo areainfo = selectAreabList.get(i).getAreainfo();
						if(areainfo.getAreanet().getKeyword().contains("NF-net-0")){
						JSONObject listdata = new JSONObject();
						listdata.put("bindingareaid",areainfo.getArea().getId());
						listdata.put("areaPic", pic+areainfo.getArea().getImgPic());
						listdata.put("areaName",areainfo.getArea().getAreaName());
						listdata.put("areaiden",areainfo.getAreanet().getId());
						listdata.put("accountName", areainfo.getAccountName());
						listdata.put("pwdName", areainfo.getPwdName());
						jsonList.put(listdata);
					}
				}
			}
			sendjson.put("code", 1);
			sendjson.put("areaInfo", jsonList.toString());
		} catch (Exception e) {
			sendjson.put("code", -1);
			e.printStackTrace();
		}
		out.print(sendjson);
	}
	
	
	/**
	 * 用户所有绑定List >Nf025
	 */
	public void bindingList(){
		org.json.JSONObject datajson = DataUtil.dataParser(params);
		
		if(datajson.has("userid"))
			userid = datajson.getInt("userid");
		
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out=null;
		net.sf.json.JSONObject json = DataUtil.sendJson();
		try {
			out = response.getWriter();
			String pic = IPUtil.getIp()+"/icon/bangding/";
			//根据用户id,此用户所有绑定信息
			List<Binding> bids = bindingService.selsctBindingUsers(userid);
			JSONArray ja = new JSONArray(); 
			if(!bids.isEmpty()){
				for (int i = 0; i < bids.size(); i++) {
					JSONObject j = new JSONObject();
					j.put("bindingid", bids.get(i).getId());
					j.put("areaName",bids.get(i).getArea().getAreaName());
					j.put("account", bids.get(i).getAccount());
					j.put("areaPic", pic+bids.get(i).getArea().getImgPic());
					ja.put(j);
				}
			}
			json.put("code", 1);
			json.put("bindingInfo", ja.toString());
			out.print(json);
		} catch (Exception e) {
			json.put("code",-1);
			out.print(json);
			e.printStackTrace();
		}
	}



	



	//SET/GET
	public void setAreaIpService(AreaIpService areaIpService) {
		this.areaIpService = areaIpService;
	}
	public void setBindingService(BindingService bindingService) {
		this.bindingService = bindingService;
	}



	public void setAreausbleService(AreausbleService areausbleService) {
		this.areausbleService = areausbleService;
	}




	public void setNF_callid(String nF_callid) {
		NF_callid = nF_callid;
	}




	public void setHotspotLink(HotspotLink hotspotLink) {
		this.hotspotLink = hotspotLink;
	}




	public void setUserinfoService(UserinfoService userinfoService) {
		this.userinfoService = userinfoService;
	}



	public void setAreaService(AreaService areaService) {
		this.areaService = areaService;
	}



	public void setAreainfoService(AreainfoService areainfoService) {
		this.areainfoService = areainfoService;
	}




	public void setAccount(String account) {
		this.account = account;
	}



	public void setAccountPwd(String accountPwd) {
		this.accountPwd = accountPwd;
	}



	public void setIp(String ip) {
		this.ip = ip;
	}



	public void setParams(String params) {
		this.params = params;
	}



	public void setUserid(int userid) {
		this.userid = userid;
	}



	public void setAreaid(int areaid) {
		this.areaid = areaid;
	}



	public void setSiteAreaid(int siteAreaid) {
		this.siteAreaid = siteAreaid;
	}



	public void setAreaiden(int areaiden) {
		this.areaiden = areaiden;
	}



	public void setBindingid(int bindingid) {
		this.bindingid = bindingid;
	}

	public void setUseraccountService(UseraccountService useraccountService) {
		this.useraccountService = useraccountService;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}
	
	

}
